// Characters (字符) 示例
// 演示 Pascal 中字符的各种用法
program p_18_characters;

{$mode objfpc}{$H+}
// {$mode objfpc} 启用 Object Pascal 模式
// {$H+} 启用长字符串支持（AnsiString）

// 引入单元文件
uses
  SysUtils;  // 系统工具单元，包含常用的字符处理函数

// 自定义字符分类函数
function IsLetter(ch: Char): Boolean;
begin
  IsLetter := ((ch >= 'A') and (ch <= 'Z')) or ((ch >= 'a') and (ch <= 'z'));
end;

function IsDigit(ch: Char): Boolean;
begin
  IsDigit := (ch >= '0') and (ch <= '9');
end;

function IsSpace(ch: Char): Boolean;
begin
  IsSpace := (ch = ' ') or (ch = #9) or (ch = #10) or (ch = #13);
end;

// 所有变量声明移到这里
var
  ch1: Char;
  ch2: Char;
  ch3: Char;
  letter: Char;
  asciiValue: Integer;
  charFromAscii: Char;
  lowerChar: Char;
  upperChar: Char;
  testChars: array[1..6] of Char;
  i: Integer;
  c: Char;
  charArray: array[1..10] of Char;
  strFromArray: String;

// 程序主入口
begin
  WriteLn('=== Pascal 字符示例 ===');
  WriteLn;
  
  // 1. 字符声明和初始化
  WriteLn('1. 字符声明和初始化:');
  ch1 := 'A';
  ch2 := #66;  // ASCII值66对应字符'B'
  ch3 := '3';
  
  WriteLn('  ch1: ', ch1);
  WriteLn('  ch2: ', ch2);
  WriteLn('  ch3: ', ch3);
  
  WriteLn;
  
  // 2. 字符编码转换
  WriteLn('2. 字符编码转换:');
  letter := 'X';
  WriteLn('  字符: ', letter);
  WriteLn('  ASCII值: ', Ord(letter));
  
  // 从编码获取字符
  asciiValue := 90;  // Z的ASCII值
  charFromAscii := Chr(asciiValue);
  WriteLn('  ASCII值 ', asciiValue, ' 对应的字符: ', charFromAscii);
  
  WriteLn;
  
  // 3. 字符比较和转换
  WriteLn('3. 字符比较和转换:');
  lowerChar := 'a';
  upperChar := UpCase(lowerChar);
  WriteLn('  小写字符: ', lowerChar);
  WriteLn('  转换为大写: ', upperChar);
  
  // 字符比较
  if 'A' < 'Z' then
    WriteLn('  ''A'' 在字母表中排在 ''Z'' 前面')
  else
    WriteLn('  ''A'' 在字母表中排在 ''Z'' 后面');
  
  WriteLn;
  
  // 4. 字符分类
  WriteLn('4. 字符分类:');
  testChars[1] := 'A';
  testChars[2] := '5';
  testChars[3] := ' ';
  testChars[4] := '.';
  testChars[5] := 'z';
  testChars[6] := #9;  // #9是制表符
  
  for i := 1 to 6 do
  begin
    Write('  字符 ''', testChars[i], ''' 是');
    
    if IsLetter(testChars[i]) then
      WriteLn(' 字母')
    else if IsDigit(testChars[i]) then
      WriteLn(' 数字')
    else if IsSpace(testChars[i]) then
      WriteLn(' 空白字符')
    else
      WriteLn(' 其他字符');
  end;
  
  WriteLn;
  
  // 5. 特殊字符
  WriteLn('5. 特殊字符:');
  WriteLn('  回车符 (#13): ''', #13, '''');
  WriteLn('  换行符 (#10): ''', #10, '''');
  WriteLn('  制表符 (#9): ''', #9, '''');
  WriteLn('  空字符 (#0): ''', #0, '''');
  
  WriteLn;
  
  // 6. 字符数组
  WriteLn('6. 字符数组:');
  charArray[1] := 'H';
  charArray[2] := 'e';
  charArray[3] := 'l';
  charArray[4] := 'l';
  charArray[5] := 'o';
  charArray[6] := ' ';
  charArray[7] := 'W';
  charArray[8] := 'o';
  charArray[9] := 'r';
  charArray[10] := 'l';
  
  Write('  字符数组: ');
  for i := 1 to 10 do
    Write(charArray[i]);
  WriteLn('d');
  
  // 将字符数组转换为字符串
  strFromArray := '';
  for i := 1 to 10 do
    strFromArray := strFromArray + charArray[i];
  strFromArray := strFromArray + 'd';
  WriteLn('  转换为字符串: ', strFromArray);
  
  WriteLn;
  
  // 7. 字符循环
  WriteLn('7. 字符循环:');
  Write('  字母A到Z: ');
  for c := 'A' to 'Z' do
    Write(c);
  WriteLn;
  
  Write('  数字0到9: ');
  for c := '0' to '9' do
    Write(c);
  WriteLn;
  
  WriteLn;
  WriteLn('演示了 Pascal 中字符的各种用法');
  
  // 等待用户按键，防止程序窗口立即关闭
  ReadLn;
end.